import os

from rest_framework import generics
from rest_framework.decorators import api_view

from rest_framework.response import Response

import books.swagger_configs
from books.models import Book, Author
from books.serializers import BookSerializer, AuthorSerializer
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

# 判断是否是生产环境
# is_production = os.getenv('MY_ENV_VAR') == 'production'

# 尝试导入 drf_yasg
# try:
#     from drf_yasg.utils import swagger_auto_schema as _swagger_auto_schema
#     from drf_yasg import openapi
#     drf_yasg_installed = True
# except ImportError:
#     # 如果 drf_yasg 没有安装，则标记为没有安装
#     drf_yasg_installed = False
#
# # 根据是否安装 drf_yasg 来应用装饰器
# def swagger_auto_schema(func=None, **kwargs):
#     if drf_yasg_installed:
#         return _swagger_auto_schema(func, **kwargs)  # 如果在开发环境且安装了 drf_yasg，应用装饰器
#     else:
#         return func  # 如果没有安装或是生产环境，直接返回原函数

class BooksListCreateApiView(generics.ListCreateAPIView):
    """
    这是一个介绍。
    """
    queryset = Book.objects.all()
    serializer_class = BookSerializer

    @swagger_auto_schema(
        operation_summary="获取所有书籍",
        operation_description="""
        这是get的介绍

        这是get的介绍

        这是get的介绍

        这是get的介绍
        """,
        responses={200: openapi.Response(
            description="这是get的返回结果",
            schema=openapi.Schema(
                type=openapi.TYPE_OBJECT,
                properties={
                    'id': openapi.Schema(type=openapi.TYPE_INTEGER, description='ID'),
                    'title': openapi.Schema(type=openapi.TYPE_STRING, description='标题'),
                    'author': openapi.Schema(type=openapi.TYPE_STRING, description='作者'),
                    'price': openapi.Schema(type=openapi.TYPE_NUMBER, description='价格'),
                    'publish_date': openapi.Schema(type=openapi.TYPE_STRING, description='出版日期')
                }
            ),
            examples={
                'application/json': [
                    {
                        "id": 2,
                        "title": "资本论",
                        "author": "马克思",
                        "publication_date": "2024-12-20",
                        "isbn": "5sd5fa2sd1f5s",
                        "publisher": "出版社",
                        "pages": 49000,
                        "price": "50.00"
                    },
                    {
                        "id": 3,
                        "title": "3333333333333",
                        "author": "马克思",
                        "publication_date": "2024-12-20",
                        "isbn": "5sd5fa2sd1f5s",
                        "publisher": "出版社",
                        "pages": 49000,
                        "price": "50.00"
                    }
                ]
            }
        )}
    )
    def get(self, request, *args, **kwargs):
        """
        这是get的介绍
        """
        return super().get(request, *args, **kwargs)

    @swagger_auto_schema(
        operation_summary="创建书籍",
        operation_description="""
        这是post的介绍

        这是post的介绍

        这是post的介绍

        这是post的介绍
        """,
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            properties={
                'title': openapi.Schema(type=openapi.TYPE_STRING, description='标题'),
                'author': openapi.Schema(type=openapi.TYPE_STRING, description='作者'),
                'price': openapi.Schema(type=openapi.TYPE_NUMBER, description='价格'),
                'publish_date': openapi.Schema(type=openapi.TYPE_STRING, description='出版日期')
            },
            required=['title', 'author']
        )
    )
    def post(self, request, *args, **kwargs):
        """
        这是post的介绍
        """
        return super().post(request, *args, **kwargs)


class AuthorListCreateApiView(generics.ListCreateAPIView):
    queryset = Author.objects.all()
    serializer_class = AuthorSerializer


@api_view(['GET'])
def get_books(request):
    books = Book.objects.all()
    serializer = BookSerializer(books, many=True)
    return Response(serializer.data)


@swagger_auto_schema(
    methods=['GET'],
    operation_description="获取所有书籍",
    # responses={200: books.swagger_configs.BOOKS3_RESPONSE},
    responses={200: 'hh'}
)
@api_view(['GET'])
def get_books3(request):
    books = Book.objects.all()
    serializer = BookSerializer(books, many=True)
    return Response(serializer.data)
